package apis

import (
	"github.com/gin-gonic/gin"
	"go-admin/models"
	"go-admin/tools"
	"go-admin/tools/app"
	"go-admin/tools/app/msg"
)

func GetZxOrderList(c *gin.Context) {
	var data models.VROrder

	var pageSize = tools.ParseFormInt1(c, "pageSize", 10)
	var pageIndex = tools.ParseFormInt1(c, "pageIndex", 1)

	pageIndex = tools.ParseFormInt(c, "pageIndex")
	data.CustomName = tools.ParseFormStr(c, "customName")
	data.CustomTel = tools.ParseFormStr(c, "customTel")
	data.CustomAddr = tools.ParseFormStr(c, "customAddr")

	data.SignState = tools.ParseFormStr(c, "signState")
	data.DesignState = tools.ParseFormStr(c, "designState")
	data.BuildState = tools.ParseFormStr(c, "buildState")
	data.RevisitState = tools.ParseFormStr(c, "revisitState")

	data.DataScope = tools.GetUserIdStr(c)
	data.CreateBy = tools.GetUserIdStr(c)

	var err error
	result, count, err := data.GetPage(pageSize, pageIndex)
	tools.HasError(err, "", -1)

	app.PageOK(c, result, count, pageIndex, pageSize, "")
}

func GetZxOrder(c *gin.Context) {
	var data models.VROrder
	data.Id, _ = tools.StringToInt(c.Param("id"))
	result, err := data.Get()
	tools.HasError(err, "抱歉未找到相关信息", -1)

	app.OK(c, result, "")
}

func InsertZxOrder(c *gin.Context) {
	var data models.ZxOrder
	err := c.ShouldBindJSON(&data)
	data.CreateBy = tools.GetUserIdStr(c)
	if len(data.CustomTel) > 11 {
		app.ErrMsg(c, "请输入11电话")
		return
	}
	tools.HasError(err, "", 500)

	if data.State == "" {
		data.State = "doing"
	}
	signId, err := InsertZxSign2(data)
	if err != nil {
		app.Err(c, err)
		return
	}
	data.SignId = signId

	designId, err := InsertZxDesign2(data)
	if err != nil {
		app.Err(c, err)
		return
	}
	data.DesignId = designId

	buildId, err := InsertZxBuild2(data)
	if err != nil {
		app.Err(c, err)
		return
	}
	data.BuildId = buildId

	revisitId, err := InsertZxRevisit2(data)
	if err != nil {
		app.Err(c, err)
		return
	}
	data.RevisitId = revisitId
	if err != nil {
		app.Err(c, err)
		return
	}
	result, err := data.Create()
	tools.HasError(err, "", -1)
	app.OK(c, result, "")
}

func InsertZxSign2(data models.ZxOrder) (int, error) {
	sign := models.ZxSign{
		//FinishDate: nil,
	}

	s, err := sign.Create()
	if err != nil {
		return 0, err
	}
	return s.Id, nil
}

func InsertZxDesign2(data models.ZxOrder) (int, error) {
	design := models.ZxDesign{
		//FinishDate: nil,
	}
	d, err := design.Create()
	if err != nil {
		return 0, err
	}
	return d.Id, nil
}

func InsertZxBuild2(data models.ZxOrder) (int, error) {
	build := models.ZxBuild{
		Id: data.Id,
		//FinishDate: nil,
	}
	b, err := build.Create()
	if err != nil {
		return 0, nil
	}
	return b.Id, nil
}

func InsertZxRevisit2(data models.ZxOrder) (int, error) {
	revisit := models.ZxRevisit{
		Id: data.Id,
		//FinishDate: tools.GetCurrntTime(),
	}
	r, err := revisit.Create()
	if err != nil {
		return 0, nil
	}
	return r.Id, nil
}

func UpdateZxOrder(c *gin.Context) {
	var data models.VROrder
	err := c.BindJSON(&data)
	tools.HasError(err, "数据解析失败", -1)
	data.UpdateBy = tools.GetUserIdStr(c)
	result, err := data.ZxOrder.Update(data.Id)
	tools.HasError(err, "", -1)
	_, err = data.UpdateOther()
	tools.HasError(err, "", -1)

	app.OK(c, result, "")
}

func DeleteZxOrder(c *gin.Context) {
	var data models.ZxOrder
	data.UpdateBy = tools.GetUserIdStr(c)

	IDS := tools.IdsStrToIdsIntGroup("id", c)
	_, err := data.BatchDelete(IDS)
	tools.HasError(err, msg.DeletedFail, 500)
	app.OK(c, nil, msg.DeletedSuccess)
}
